package com.wangrui.test;

import com.wangrui.dao.UserMapper8;
import com.wangrui.entity.SUser;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.ExecutorType;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.junit.Test;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.UUID;

public class MyBatisTest08 {

    /**
     * 获得MyBatis SqlSessionFactory
     * SqlSessionFactory负责创建SqlSession，一旦创建成功，就可以用SqlSession实例来执行映射语句
     * ，commit，rollback，close等方法。
     *
     * @return
     */
    private static SqlSessionFactory getSessionFactory() {
        SqlSessionFactory sessionFactory = null;
        String resource = "configuration.xml";
        try {
            sessionFactory = new SqlSessionFactoryBuilder().build(Resources.getResourceAsReader(resource));
        } catch (IOException e) {

            e.printStackTrace();
        }
        return sessionFactory;
    }


    @Test
    public void testDynamic() {
        SqlSessionFactory factory = getSessionFactory();
        SqlSession session = factory.openSession();
        UserMapper8 userMapper = session.getMapper(UserMapper8.class);
        List<SUser> list = userMapper.select1("tom");
        for (SUser user : list) {
            System.out.println(user);
        }
        session.close();
    }


    @Test
    public void batchInsert() {
        //不需要allowMultiQueries=true
        SqlSessionFactory factory = getSessionFactory();
        SqlSession session = factory.openSession(ExecutorType.BATCH);
        UserMapper8 userMapper = session.getMapper(UserMapper8.class);
        for (int i = 0; i < 10; i++) {
            SUser user = new SUser();
            user.setUserName(UUID.randomUUID().toString());
            user.setUserBirthday(new Date());
            user.setUserSalary(45.6f);
            userMapper.insert1(user);
            System.out.println("获取自增主见:" + user.getUserId());//因为采用批量操作，此时并没有真正发送请求，所以自增主见为null
        }
        session.commit();//最后提交，如果每一次都提交一次，批量操作就变成了顺序操作
        session.close();
    }

    @Test
    public void batchInsert2() {
        //需要allowMultiQueries=true
        SqlSessionFactory factory = getSessionFactory();
        SqlSession session = factory.openSession();
        UserMapper8 userMapper = session.getMapper(UserMapper8.class);
        List<SUser> userList = new ArrayList<>();
        for (int i = 0; i < 1000; i++) {
            SUser user = new SUser();
            user.setUserName(UUID.randomUUID().toString());
            user.setUserBirthday(new Date());
            user.setUserSalary(45.6f);
            userList.add(user);
        }
        userMapper.insert2(userList);
        session.commit();
        session.close();
    }

    @Test
    public void batchInsert3() {
        //不需要allowMultiQueries=true
        SqlSessionFactory factory = getSessionFactory();
        SqlSession session = factory.openSession();
        UserMapper8 userMapper = session.getMapper(UserMapper8.class);
        List<SUser> userList = new ArrayList<>();
        for (int i = 0; i < 10000; i++) {
            SUser user = new SUser();
            user.setUserName(UUID.randomUUID().toString());
            user.setUserBirthday(new Date());
            user.setUserSalary(100f);
            userList.add(user);
        }
        userMapper.insert3(userList);
        session.commit();
        session.close();
    }

    @Test
    public void testGeneratedKeys() {
        SqlSessionFactory factory = getSessionFactory();
        SqlSession session = factory.openSession();
        UserMapper8 userMapper = session.getMapper(UserMapper8.class);
        SUser user = new SUser();
        user.setUserName(UUID.randomUUID().toString());
        user.setUserBirthday(new Date());
        user.setUserSalary(45.6f);
        userMapper.insert1(user);
        System.out.println("获取自增主见:" + user.getUserId());
        session.commit();
        session.close();
    }
}
